<template>
  <div class="login">
    <div id="loginbg">
      <div class="container">
        <el-form
          :model="loginUser"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="loginForm"
        >
          <el-form-item label="手机号" prop="tel" ref="tel">
            <el-input v-model="loginUser.tel"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="loginUser.password"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button @click="resetForm('ruleForm')">取消</el-button>
          </el-form-item>
          <!-- <router-link to="/register">
            <div id="toregister">还没有注册？马上去注册</div>
          </router-link>-->
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import jwt_decode from "jwt-decode";
export default {
  name: "login",
  data: function() {
    return {
      loginUser: {
        tel: "18045606588",
        password: "123456"
      },
      rules: {
        tel: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          { min: 11, max: 11, message: "手机号码必须是11位", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 6, message: "密码必须是6位", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      //通过ref定位到form表单
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$axios
            .post("http://localhost:3000/users/login", this.loginUser)
            .then(res => {
              console.log(res.status);
              if (res.data.msg == "登录成功！！") {
                this.$message({
                  message: "登陆成功",
                  type: "success"
                });
                console.log("登录成功", res);
                localStorage.setItem("mytoken", res.data.token);
                this.$router.push("/index");
              }
              if (res.data.msg == "密码错误，登录失败！！") {
                alert("密码错误，登录失败！");
              }
              if (res.data.msg == "手机号不存在，登录失败！") {
                alert("手机号不存在，登录失败！");
              }
            });
        } else {
          console.log("error submit!!");
          alert("登录失败！");
          return false;
        }
      });
    },
    resetForm(formName) {
      //实现重置表单元素数据
      this.$refs[formName].resetFields();
      this.$router.push("/index");
    }
  }
};
</script>
<style lang="css" scoped>
.login {
  width: 100%;
  height: 650px;
  padding: 0;
  background-image: url("../assets/GYQ/slider1.jpg");
  background-position: center center;
  background-size: 100% 100%;
}
#loginbg {
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.466);
}
.container {
  height: 100%;
  padding-top: 150px;
  padding-left: 55%;
}

.loginForm {
  width: 400px;
  height: 370px;
  border-radius: 10px;
  padding: 80px 50px 10px 10px;
  background-color: white;
}
.loginForm:hover {
  box-shadow: 0 0 10px 6px rgba(160, 146, 146, 0.5);
}
@media (max-width: 769px) {
  .loginForm {
    height: 100%;
    width: 100%;
    border-radius: 0px;
  }
  .container {
    height: 100%;
    padding: 0;
  }
}
</style>

